<?php
/**
 * 商品类
 * @author 孙银川
 * @date   2017.12.25
 */

namespace app\common\model;
use think\Model;
use PHPExcel;
use PHPExcel_IOFactory;

class Goods extends Model{

	/**
     * 商品数据
     */
	public function enquiries($num, $map=[]){
		// 关联商品分类、商户表
		$data = db('goods')
			->alias('g')
			->leftJoin('shop_gcate c','g.gd_cate_id=c.sg_id')
			->leftJoin('shops s','g.gd_shop_id=s.sp_id')
			->field('g.gd_id,g.gd_name,c.sg_name,g.gd_price,g.gd_status,g.gd_audit,s.sp_name,g.gd_sale_time,g.gd_create_time')
			->where($map)
			->paginate($num);
		// 分页
		$data->page = $data->render();
		// 统计
		$data->count = db('goods')->alias('g')->where($map)->count();
		return $data;
	}

	/**
     * 商品详情
     */
	public function detail($id){
		// 关联商品分类、商户表
		$data = db('goods')
			->alias('g')
			->leftJoin('shop_gcate c','g.gd_cate_id=c.sg_id')
			->leftJoin('shops s','g.gd_shop_id=s.sp_id')
			->field('g.*,c.sg_name,s.sp_name')
			->where('g.gd_id='.$id)
			->find();
		return $data;
	}

	/**
     * 添加商品
     */
	public function add($data){
		$res = db('goods')->insert($data);
		return $res;
	}

	/**
     * 修改商品
     */
	public function edit($data=[]){
		$res = db('goods')->update($data);
		return $res;
	}

	/**
     * 获取商户下的商品分类
     */
	public function cates($shop_id, $map=[]){
		$data = db('shop_gcate')->where('sg_shop_id='.$shop_id)->where($map)->select();
		return $data;
	}

	/**
     * 添加商品分类
     */
	public function add_cate($data){
		$res = db('shop_gcate')->insert($data);
		return $res;
	}

	/**
     * 修改商品分类
     */
	public function upd_cate($data){
		$res = db('shop_gcate')->update($data);
		return $res;
	}

	/**
     * 商品分类详情
     */
	public function cate($id){
		$res = db('shop_gcate')->where('sg_id='.$id)->find();
		return $res;
	}

	/**
     * 获取商户下的所有商品
     */
	public function shop_goods($shop_id){
		$data = db('goods')->field('gd_id,gd_name,gd_image_url,gd_cate_id,gd_price,gd_sort')->where('gd_shop_id='.$shop_id.' and gd_audit=1 and gd_status=1')->select();
		return $data;
	}
}
?>